package com.beetobe.main;

//import static com.beetobe.main.CommonUtilities.SENDER_ID;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.beetobe.callib.CalendarLibHelper;
import com.google.android.gcm.GCMRegistrar;

@SuppressLint("ParserError")
public class LoginActivity extends Activity {
	static final String SENDER_ID = "656478210404";

	private static final String TAG = "LoginActivity";
	// String GCM_ID;
	String gcmIdPass;

	String registrationID;
	String calendarID;
	// ProgressDialog pDlg;

	// private String regID1;

	

	@SuppressLint("ParserError")
	public void onCreate(Bundle savedInstanceState) {
		
		startActivity(new Intent(this, SplashActivity.class));

		
		super.onCreate(savedInstanceState);
		
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_login);
		String gcmIDFinal = null;
		Intent intent = getIntent();
		
		if (intent.getData() != null) {
			gcmIDFinal = intent.getData().toString();

			calendarID = gcmIDFinal.split("//")[1];

			registrationID = gcmIDFinal.split("//")[2];

			Toast.makeText(this, calendarID + ", " + registrationID,
					Toast.LENGTH_SHORT).show();
		}
//		gcmIdPass = intent.getStringExtra(Intent.EXTRA_TEXT);

		String nullString = null;
		if (gcmIdPass == null)
			nullString = "NULL";
		else
			nullString = gcmIdPass;

		// settings = getPreferences(MODE_PRIVATE);
		// accountManager = new GoogleAccountManager(this);
		// accountName = settings.getString(PREF_ACCOUNT_NAME, null);

		
		Button loginBtn = (Button) findViewById(R.id.loginBtn);

		// Account account = accountManager.getAccountByName(accountName);

		// if (account != null) {
		// Intent intent = new Intent(LoginActivity.this, MainActivity.class);
		// startActivity(intent);
		// }

		// checkNotNull(SENDER_ID, "SENDER_ID");
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);

		CalendarLibHelper.myregId = getSharedPreferences("myregId",
				MODE_PRIVATE);
		// final String regId = GCMRegistrar.getRegistrationId(this);
		String regId = CalendarLibHelper.myregId.getString("regId", "");

		if (regId == "") {

			// Log.i(TAG, "registration id =====&nbsp; "+regId);

			GCMRegistrar.register(this, SENDER_ID);

			SharedPreferences.Editor ed = CalendarLibHelper.myregId.edit();
			ed.putString("regId", GCMRegistrar.getRegistrationId(this));

			ed.commit();

		} else {
			 
			// Log.v(TAG, "Already registered");
		}
		// final String regId = GCMRegistrar.getRegistrationId(this);
		// Log.i(TAG, "registration id =====&nbsp; "+regId);

		// if (regId.equals("")) {
		// GCMRegistrar.register(this, SENDER_ID);
		// SharedPreferences.Editor ed = myregId.edit();
		// ed.putString("myregId", regId);

		// ed.commit();
		// } else {
		// Log.v(TAG, "Already registered");
		// }

		// Log.i(TAG, "regId; "+regId);

		loginBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this,
						MainActivity.class);
				if (registrationID != null && calendarID != null) {
					intent.putExtra("REG_ID", registrationID);
					intent.putExtra("CAL_ID", calendarID);

					
				}

				startActivity(intent);
			}
		});
	}

	
}
